<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>论文检测系统 - 登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --rococo-pink: #F8E1E4;
            --rococo-blue: #E1E8F0;
            --rococo-green: #E8F0E1;
            --rococo-gold: #D4AF37;
            --rococo-purple: #E8E1F0;
            --rococo-text: #6B4E71;
            --rococo-shadow: rgba(107, 78, 113, 0.1);
        }

        body {
            min-height: 100vh;
            background: linear-gradient(135deg, var(--rococo-pink) 0%, var(--rococo-blue) 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            position: relative;
            overflow: hidden;
        }

        /* 添加装饰性背景图案 */
        body::before {
            content: '';
            position: absolute;
            width: 200%;
            height: 200%;
            background-image: 
                radial-gradient(circle at 25% 25%, var(--rococo-shadow) 1px, transparent 1px),
                radial-gradient(circle at 75% 75%, var(--rococo-shadow) 1px, transparent 1px);
            background-size: 50px 50px;
            opacity: 0.3;
            animation: backgroundMove 60s linear infinite;
            z-index: 0;
        }

        @keyframes backgroundMove {
            0% { transform: translate(0, 0); }
            100% { transform: translate(-50px, -50px); }
        }

        .container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 15px 35px var(--rococo-shadow);
            padding: 40px;
            width: 100%;
            max-width: 420px;
            position: relative;
            z-index: 1;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 5px;
            background: linear-gradient(90deg, var(--rococo-pink), var(--rococo-blue), var(--rococo-green));
            border-radius: 20px 20px 0 0;
        }

        .form-title {
            text-align: center;
            font-size: 1.8rem;
            color: var(--rococo-text);
            margin-bottom: 25px;
            font-weight: 300;
            letter-spacing: 1px;
        }

        .form-container {
            /* non-active forms are hidden by opacity and pointer-events */
            opacity: 0;
            transform: translateY(20px);
            transition: all 0.5s ease-out;
            pointer-events: none;
            position: absolute; /* Take form out of flow to allow smooth transition */
            top: 40px; /* Align with container padding */
            left: 40px;
            right: 40px;
        }

        .form-container.active {
            opacity: 1;
            transform: translateY(0);
            pointer-events: auto;
            position: relative; /* Bring active form back into flow */
            top: auto;
            left: auto;
            right: auto;
        }

        .form-group {
            margin-bottom: 25px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: var(--rococo-text);
            font-weight: 500;
            font-size: 0.95rem;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid var(--rococo-blue);
            border-radius: 10px;
            font-size: 1rem;
            transition: all 0.3s ease;
            background: rgba(255, 255, 255, 0.9);
            color: var(--rococo-text);
        }

        .form-group input:focus {
            border-color: var(--rococo-gold);
            box-shadow: 0 0 0 3px rgba(212, 175, 55, 0.1);
            outline: none;
            background: #fff;
        }

        .form-group input::placeholder {
            color: #aaa;
        }

        .error-message {
            color: #dc3545;
            font-size: 0.85rem;
            margin-top: 5px;
            min-height: 20px;
            opacity: 0;
            transform: translateY(-10px);
            transition: all 0.3s ease;
        }

        .error-message.show {
            opacity: 1;
            transform: translateY(0);
        }

        .submit-btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(135deg, var(--rococo-gold) 0%, #C5A028 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
        }

        .submit-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(212, 175, 55, 0.3);
        }

        .submit-btn:active {
            transform: translateY(0);
        }

        .submit-btn::after {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 5px;
            height: 5px;
            background: rgba(255, 255, 255, 0.5);
            opacity: 0;
            border-radius: 100%;
            transform: scale(1, 1) translate(-50%);
            transform-origin: 50% 50%;
        }

        .submit-btn:focus:not(:active)::after {
            animation: ripple 1s ease-out;
        }

        @keyframes ripple {
            0% { transform: scale(0, 0); opacity: 0.5; }
            100% { transform: scale(20, 20); opacity: 0; }
        }

        .switch-form {
            text-align: center;
            margin-top: 20px;
            color: var(--rococo-text);
            font-size: 0.9rem;
        }

        .switch-form a {
            color: var(--rococo-gold);
            text-decoration: none;
            font-weight: 600;
            margin-left: 5px;
            transition: color 0.3s ease;
        }

        .switch-form a:hover {
            color: #C5A028;
            text-decoration: underline;
        }

        .loading {
            display: none;
            width: 30px;
            height: 30px;
            border: 3px solid var(--rococo-blue);
            border-top: 3px solid var(--rococo-gold);
            border-radius: 50%;
            margin: 20px auto;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .success-message {
            display: none;
            text-align: center;
            color: var(--rococo-text);
            font-size: 0.9rem;
            margin-top: 10px;
            padding: 10px;
            background: rgba(232, 240, 225, 0.5);
            border-radius: 5px;
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-10px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* 添加装饰性元素 */
        .form-container::before {
            content: '';
            position: absolute;
            top: -10px;
            left: -10px;
            right: -10px;
            bottom: -10px;
            border: 1px solid var(--rococo-gold);
            border-radius: 20px;
            opacity: 0.2;
            pointer-events: none;
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .container {
                padding: 30px 20px;
            }

            .form-group input {
                padding: 10px 12px;
            }

            .submit-btn {
                padding: 12px;
            }
        }

        /* 暗色模式支持 */
        @media (prefers-color-scheme: dark) {
            :root {
                --rococo-pink: #2C1B2E;
                --rococo-blue: #1B2E2C;
                --rococo-green: #1B2E1B;
                --rococo-gold: #D4AF37;
                --rococo-purple: #2E1B2E;
                --rococo-text: #E8E1F0;
                --rococo-shadow: rgba(0, 0, 0, 0.2);
            }

            .container {
                background: rgba(30, 30, 30, 0.95);
            }

            .form-group label {
                color: var(--rococo-text);
            }

            .form-group input {
                background: rgba(40, 40, 40, 0.9);
                border-color: var(--rococo-blue);
                color: var(--rococo-text);
            }

            .form-group input:focus {
                background: rgba(50, 50, 50, 0.9);
            }

            .form-group input::placeholder {
                color: #888;
            }

            .switch-form {
                color: var(--rococo-text);
            }

            .loading {
                border-color: var(--rococo-blue);
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 登录表单 -->
        <div id="loginForm" class="form-container active">
            <h2 class="form-title">登录</h2>
            <div class="form-group">
                <label for="loginUsername">用户名</label>
                <input type="text" id="loginUsername" placeholder="请输入用户名">
                <div class="error-message" id="loginUsernameError"></div>
            </div>
            <div class="form-group">
                <label for="loginPassword">密码</label>
                <input type="password" id="loginPassword" placeholder="请输入密码">
                <div class="error-message" id="loginPasswordError"></div>
            </div>
            <button class="submit-btn" onclick="handleLogin()">登录</button>
            <div class="loading" id="loginLoading"></div>
            <div class="switch-form">
                没有账户？ <a href="#" onclick="event.preventDefault(); switchForm('register')">立即注册</a>
            </div>
        </div>

        <!-- 注册表单 -->
        <div id="registerForm" class="form-container">
            <h2 class="form-title">注册</h2>
            <div class="form-group">
                <label for="registerUsername">用户名</label>
                <input type="text" id="registerUsername" placeholder="请输入用户名">
                <div class="error-message" id="registerUsernameError"></div>
            </div>
            <div class="form-group">
                <label for="registerEmail">电子邮箱</label>
                <input type="email" id="registerEmail" placeholder="请输入电子邮箱">
                <div class="error-message" id="registerEmailError"></div>
            </div>
            <div class="form-group">
                <label for="registerPassword">密码</label>
                <input type="password" id="registerPassword" placeholder="请输入密码">
                <div class="error-message" id="registerPasswordError"></div>
            </div>
            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" placeholder="请再次输入密码">
                <div class="error-message" id="confirmPasswordError"></div>
            </div>
            <button class="submit-btn" onclick="handleRegister()">注册</button>
            <div class="loading" id="registerLoading"></div>
            <div class="switch-form">
                已有账户？ <a href="#" onclick="event.preventDefault(); switchForm('login')">立即登录</a>
            </div>
        </div>
    </div>

    <script>
        const loginForm = document.getElementById('loginForm');
        const registerForm = document.getElementById('registerForm');
        const container = document.querySelector('.container');

        function switchForm(form) {
            clearErrors();
            if (form === 'login') {
                loginForm.classList.add('active');
                registerForm.classList.remove('active');
            } else {
                loginForm.classList.remove('active');
                registerForm.classList.add('active');
            }
        }

        // 显示错误信息
        function showError(elementId, message) {
            const errorElement = document.getElementById(elementId);
            errorElement.textContent = message;
            errorElement.classList.add('show');
        }

        // 清除错误信息
        function clearErrors() {
            document.querySelectorAll('.error-message').forEach(el => {
                el.classList.remove('show');
                el.textContent = '';
            });
        }

        // 显示加载动画
        function showLoading(formType) {
            document.getElementById(`${formType}Loading`).style.display = 'block';
        }

        // 隐藏加载动画
        function hideLoading(formType) {
            document.getElementById(`${formType}Loading`).style.display = 'none';
        }

        // 处理登录
        async function handleLogin() {
            clearErrors();
            const username = document.getElementById('loginUsername').value.trim();
            const password = document.getElementById('loginPassword').value.trim();

            // 表单验证
            if (!username) {
                showError('loginUsernameError', '请输入用户名');
                return;
            }
            if (!password) {
                showError('loginPasswordError', '请输入密码');
                return;
            }

            showLoading('login');

            try {
                const response = await fetch('http://localhost:8080/api/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });

                const data = await response.json();

                if (response.ok) {
                    // 保存token和用户名
                    localStorage.setItem('token', data.token);
                    localStorage.setItem('username', username);
                    // 跳转到主页
                    window.location.href = '页面.html';
                } else {
                    showError('loginUsernameError', data.message || '登录失败');
                }
            } catch (error) {
                showError('loginUsernameError', '网络错误，请稍后重试');
            } finally {
                hideLoading('login');
            }
        }

        // 处理注册
        async function handleRegister() {
            clearErrors();
            const username = document.getElementById('registerUsername').value.trim();
            const email = document.getElementById('registerEmail').value.trim();
            const password = document.getElementById('registerPassword').value.trim();
            const confirmPassword = document.getElementById('confirmPassword').value.trim();

            // 表单验证
            if (!username) {
                showError('registerUsernameError', '请输入用户名');
                return;
            }
            // 简单的邮箱格式验证
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!email || !emailRegex.test(email)) {
                showError('registerEmailError', '请输入有效的电子邮箱地址');
                return;
            }
            if (!password) {
                showError('registerPasswordError', '请输入密码');
                return;
            }
            if (password.length < 6) {
                showError('registerPasswordError', '密码长度至少为6位');
                return;
            }
            if (password !== confirmPassword) {
                showError('confirmPasswordError', '两次输入的密码不一致');
                return;
            }

            showLoading('register');

            try {
                console.log('开始注册请求...');
                console.log('请求数据:', { username, email, password });

                const response = await fetch('http://localhost:8080/api/auth/register', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    },
                    body: JSON.stringify({ username, email, password })
                });

                console.log('收到响应:', response.status, response.statusText);
                
                const responseData = await response.json();
                console.log('响应数据:', responseData);

                if (response.ok) {
                    alert(responseData.message || '注册成功！请登录。');
                    // 清空表单
                    document.getElementById('registerUsername').value = '';
                    document.getElementById('registerEmail').value = '';
                    document.getElementById('registerPassword').value = '';
                    document.getElementById('confirmPassword').value = '';
                    // 切换到登录页
                    switchForm('login');
                } else {
                    // 显示具体的错误信息
                    showError('registerUsernameError', responseData.message || '注册失败，请稍后重试');
                }
            } catch (error) {
                console.error('注册请求出错:', error);
                let errorMessage = '网络错误，请稍后重试';
                
                // 处理具体的网络错误
                if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {
                    errorMessage = '无法连接到服务器，请检查网络连接或服务器是否运行';
                }
                
                showError('registerUsernameError', errorMessage);
            } finally {
                hideLoading('register');
            }
        }

        // 添加回车键提交功能
        document.getElementById('loginPassword').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                handleLogin();
            }
        });

        document.getElementById('confirmPassword').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                handleRegister();
            }
        });

        // 添加网络状态检查
        window.addEventListener('online', function() {
            console.log('网络已连接');
        });

        window.addEventListener('offline', function() {
            console.log('网络已断开');
            showError('registerUsernameError', '网络连接已断开，请检查网络设置');
        });

        // 页面加载时检查服务器连接
        window.onload = async function() {
            try {
                const response = await fetch('http://localhost:8080/api/auth/health', {
                    method: 'GET',
                    headers: {
                        'Accept': 'application/json'
                    }
                });
                console.log('服务器状态:', response.status);
            } catch (error) {
                console.error('服务器连接检查失败:', error);
                showError('registerUsernameError', '无法连接到服务器，请确保服务器已启动');
            }
        };
    </script>
</body>
</html> 